package com.xyy.saas.payment.util;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 从apollo中获取配置的属性的值
 * @Author: wuqiongda
 * @Date: 2019/11/12 19:27
 */
@Component
public class ApolloUtil  implements EnvironmentAware {

    static Environment environment;

    public static final String EMPTY_STRING = "";

    @Override
    public void setEnvironment(Environment environment) {
        ApolloUtil.environment = environment;
    }

    /**
     * 获取配置值, 如果不存在会返回空串
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return environment.getProperty(key, EMPTY_STRING);
    }

    /**
     * 获取配置值, 如果不存在会返回传入的默认值
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue){
        return environment.getProperty(key, defaultValue);
    }

    /**
     * 获取配置值, 如果不存在会返回NULL
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public static<T> T getProperty(String key, Class<T> clazz){
        return environment.getProperty(key, clazz, null);
    }

    /**
     * 获取配置值, 如果不存在会返回传入默认值
     * @param key
     * @param clazz
     * @param defaultValue
     * @param <T>
     * @return
     */
    public static<T> T getProperty(String key, Class<T> clazz, T defaultValue){
        return environment.getProperty(key, clazz, defaultValue);
    }
}
